#CMD_PATH = D:\eclipse\opt\sparc-elf-3.4.4-mingw\bin
#CC = $(CMD_PATH)\sparc-elf-gcc
#AS = $(CMD_PATH)\sparc-elf-as
#OBJCOPY = $(CMD_PATH)\sparc-elf-objcopy
#OBJDUMP = $(CMD_PATH)\sparc-elf-objdump
#SIZE = $(CMD_PATH)\sparc-elf-size
CC = sparc-elf-gcc
AS = sparc-elf-as
OBJCOPY = sparc-elf-objcopy
OBJDUMP = sparc-elf-objdump
SIZE = sparc-elf-size
TARGET_SREC = sdram.srec
TARGET_ELF = sdram.elf
TARGET_HEX = sdram.hex
MAP_FILE = $(TARGET_ELF:.elf=.map)
SREC_FORMAT = srec
IHEX_FORMAT = ihex
CFLAGS = -nostdlib -Wall -mv8 -g -msoft-float
LDSCRIPT = sh.x
LDFLAGS = -T $(LDSCRIPT) -O2 -mv8 -nostartfiles -nostdlib -Wl,-Map,$(MAP_FILE) 
LIBS = 
SRCS = main.c irq_gpio.c irq_gptimer.c irq_uart.c irq_int0.c
OBJS = main.o irq_gpio.o irq_gptimer.o irq_uart.o irq_int0.o
SS   = main.s irq_gpio.s irq_gptimer.s irq_uart.s irq_int0.s


#all : sdram.mif
all : sdram.bin

locore.o: locore.S
	$(CC) -c $<
	
memcpy.o: memcpy.S
	$(CC) -c $<
	
memset.o: memset.S
	$(CC) -c $<
	
nop.o: nop.S
	$(CC) -c $<	
			
$(OBJS)	: $(SRCS)
	$(CC) -c $(CFLAGS) $(SRCS)

$(TARGET_ELF) : $(OBJS) locore.o memcpy.o memset.o nop.o
	$(CC) $(LDFLAGS) locore.o memcpy.o memset.o $(OBJS) nop.o -o $(TARGET_ELF) 
	$(SIZE) -Ax $(TARGET_ELF) 
	
sdram.bin : $(TARGET_ELF)
	$(OBJCOPY) -v -O $(SREC_FORMAT) $(TARGET_ELF) $(TARGET_SREC)
	$(OBJCOPY) -v -O $(IHEX_FORMAT) $(TARGET_ELF) $(TARGET_HEX)
	$(OBJCOPY) -v -O binary --only-section=.data $(TARGET_ELF)  sdram.data
	$(OBJCOPY) -v -O binary --remove-section=.data $(TARGET_ELF)  sdram.code
	copy sdram.code/b + sdram.data /b sdram.bin
	del sdram.code sdram.data
	$(OBJDUMP) $(TARGET_ELF) -S -a >lis

#sdram.mif: sdram.bin
#	.\bin2mif sdram.bin sdram.mif	
#	.\bin2vhd 12 32 ram32 ram32.vhd temp.vhd sdram.bin	
#	.\bin2v_uart 4 0 load_code load_code.v temp.v sdram.bin
#	.\bin2whyp 0 code.whyp sdram.bin		
	

clean :
	del *.o *.hex *.srec *.bin *.mif *.elf *.bak lis *.map

%.o : %.c
	$(CC) -c $(CFLAGS) $< 
%.o : %.S
	$(CC) -c $(CFLAGS) $<
%.o : %.s
	$(CC) -c $(CFLAGS) $<-

